﻿//创建时间: 2011-04-25
//创建作者: 李 城
//功能说明: CMS安装程序
//最后修改: 2011-04-25
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;


using TRPOP.CMS.Model;
using TRPOP.CMS.Business;
using TRPOP.CMS.Plugin;
using TRPOP.CMS.Log;
using System.Text;
using System.IO;
using System.Collections;
using TRPOP.Common;

namespace TRPOP.CMS.Web.ExtraBus
{
    /// <summary>
    /// 逻辑处理继承的基类
    /// </summary>
    public class Page_InstalBase : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            if (!RequestHelper.RequestString("test").IsEmpty())
            {
                Response.Write("OK");
                Response.End();
                return;
            }
            base.OnInit(e);
        }

        /// <summary>
        /// 检查Bin文件
        /// </summary>
        /// <param name="error"></param>
        /// <returns></returns>
        public static string IISSystemBINCheck(ref bool error)
        {
            string binfolderpath = HttpRuntime.BinDirectory;
            string result = "";
            try
            {
                string[] assemblylist = new string[] { 
                    "TRPOP.CMS.Web.UI.dll", 
                    "TRPOP.CMS.Web.ExtraBus.dll",
                    "TRPOP.CMS.Web.dll",
                    "TRPOP.CMS.UserManage.dll", 
                    "TRPOP.CMS.Template.dll", 
                    "TRPOP.CMS.Publish.dll",
                    "TRPOP.CMS.Model.dll",
                    "TRPOP.CMS.DALSQLServer.dll",
                    "TRPOP.CMS.Controls.dll",
                    "TRPOP.Common.dll",
                    "TRPOP.CMS.Business.dll",
                    "VS.Tools.dll",
                    "Newtonsoft.Json.dll",
                    "Microsoft.Practices.EnterpriseLibrary.Data.dll",
                    "Microsoft.Practices.EnterpriseLibrary.Caching.dll",
                    "CKFinder.dll",};
                bool isAssemblyInexistence = false;
                ArrayList inexistenceAssemblyList = new ArrayList();
                foreach (string assembly in assemblylist)
                {
                    if (!File.Exists(binfolderpath + assembly))
                    {
                        isAssemblyInexistence = true;
                        error = true;
                        inexistenceAssemblyList.Add(assembly);
                    }
                }
                if (isAssemblyInexistence)
                {
                    foreach (string assembly in inexistenceAssemblyList)
                    {
                        result += "<li><cite><img src=\"images/error.gif\" alt=\"失败\"/></cite><a href='#'>" + assembly + " 文件放置不正确</a><p>请将所有的dll文件复制到目录 " + binfolderpath + " 中.</p></li>";
                    }
                }
            }
            catch
            {
                result += "<li><cite><img src=\"images/error.gif\" alt=\"失败\"/></cite><a href='#'>请将所有的dll文件复制到目录 " + binfolderpath + " 中.</a></li>";
                error = true;
            }

            return result;
        }

        /// <summary>
        /// 系统文件夹测试
        /// </summary>
        /// <param name="foldername"></param>
        /// <returns></returns>
        public static bool SystemFolderCheck(string foldername)
        {
            string physicsPath = Common.Util.GetMapPath(foldername);
            try
            {
                using (FileStream fs = new FileStream(physicsPath + "\\a.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    fs.Close();
                }
                if (File.Exists(physicsPath + "\\a.txt"))
                {
                    System.IO.File.Delete(physicsPath + "\\a.txt");
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 系统文件测试
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static bool SystemFileCheck(string filename)
        {
            filename = Common.Util.GetMapPath(filename);
            try
            {
                if (filename.IndexOf("Temp.config") == -1 && !File.Exists(filename))
                    return false;
                if (filename.IndexOf("Temp.config") != -1)  //做删除测试
                {
                    File.Delete(filename);
                    return true;
                }
                StreamReader sr = new StreamReader(filename);
                string content = sr.ReadToEnd();
                sr.Close();
                content += " ";
                StreamWriter sw = new StreamWriter(filename, false);
                sw.Write(content);
                sw.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <returns></returns>
        public static bool TempTest()
        {
            string UserGuid = Guid.NewGuid().ToString();
            string TempPath = Path.GetTempPath();
            string path = TempPath + UserGuid;
            try
            {

                using (StreamWriter sw = new StreamWriter(path))
                {
                    sw.WriteLine(DateTime.Now);
                }

                using (StreamReader sr = new StreamReader(path))
                {
                    sr.ReadLine();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
    }
}
